<?php

namespace app\client\controller;

use app\common\controller\Frontend;
use app\common\model\banxi\student\Student;
use app\common\service\WeAppletService;

class SubscribeMsg extends Frontend
{
    protected array $noNeedLogin = ['sendSubscribeMessage'];

    protected array $noNeedPermission = ['*'];

    public function initialize(): void
    {
        parent::initialize();
    }

    public function getSubscribeNum()
    {
        $studentModel = new Student();
        $subscribeNum = $studentModel->where('id', $this->auth->id)->value('subscribeNum');
        $subscribeInfo = [
            'subscribeNum' => $subscribeNum,
            'temIds' => [env('SUBSCRIBE_MESSAGE.TEMPLATE_ID')]
        ];
        $this->ok('ok', $subscribeInfo);
    }

    public function subscribe()
    {
        $studentModel = new Student();
        // 使用数据库原子操作，避免并发问题
        $result = $studentModel->where('id', $this->auth->id)->setInc('subscribeNum', 1);

        $subscribeNum = 0;
        if ($result) {
            // 查询更新后的值
            $subscribeNum = $studentModel->where('id', $this->auth->id)->value('subscribeNum');
        }
        $this->ok('ok', $subscribeNum);

    }


}